﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extensil.Common.CategoryHandler;
using System.Configuration;

namespace Extensil.Common
{
    public class BaseCategory : UserForm
    {
        List<BaseTool> _tools = null;
        public int Order;
        public List<BaseTool> Tools
        {
            get
            {
                if(_tools == null)
                {
                    _tools = new List<BaseTool>();
                    if(Setting != null)
                        _tools.Add(Setting);
                    if(Help != null)
                        _tools.Add(Help);
                }
                return _tools;
            }
        }
        public BaseTool GetTool(string attributeName)
        {
            foreach(BaseTool bTool in Tools)
            {
                foreach(object attr in bTool.GetType().GetCustomAttributes(typeof(ToolAttribute), true))
                {
                    ToolAttribute atr = attr as ToolAttribute;
                    if(atr.Name == attributeName)
                    {
                        return bTool;
                    }
                }
            }
            foreach(BaseTool bTool in Tools)
            {
                if(bTool.GetType().Name == attributeName)
                {
                    return bTool;
                }
            }
            return null;
        }
        public List<string> ToolNames
        {
            get
            {
                List<string> retValue = new List<string>();
                foreach(BaseTool bTool in this.Tools)
                {
                    string name = bTool.GetType().Name;
                    foreach(object attr in bTool.GetType().GetCustomAttributes(typeof(ToolAttribute), true))
                    {
                        ToolAttribute atr = attr as ToolAttribute;
                        name = atr.Name;
                    }
                    retValue.Add(name);
                }
                return retValue;
            }
        }
        public void AddTool(BaseTool tool)
        {
            Tools.Add(tool);
        }
        public virtual BaseSetting Setting
        {
            get
            {
                return null;
            }
        }
        public virtual BaseHelp Help
        {
            get
            {
                return null;
            }
        }
        public void SetName(string name)
        {
            _name = name;
        }
        string _name = String.Empty;
        public new string Name
        {
            get
            {
                if(_name != String.Empty)
                    return _name;
                foreach(object attr in this.GetType().GetCustomAttributes(typeof(CategoryAttribute), true))
                {
                    CategoryAttribute atr = attr as CategoryAttribute;
                    return atr.Name;
                }
                return this.GetType().Name;
            }
            set
            {
                base.Name = value;
            }
        }
        public void ReportProgress(int progress, string message)
        {
            try
            {
                ((ISuperForm)this.SuperForm).ReportStatus(progress, message);
            }
            catch(Exception exc)
            {
            }
        }
        public void AddUpdaeApplicationSettingParameter(string key, string value)
        {
            try
            {
                using(ConfigModifier cm = new ConfigModifier())
                {
                    cm.AddUpdateApplicationSettingParameter(this.Name, key, value);
                }
            }
            catch(CategorySectionNotDefined exc)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if(config.AppSettings.Settings[key] == null)
                    config.AppSettings.Settings.Add(key, value);
                else
                    config.AppSettings.Settings[key].Value = value;
                config.Save();
                ConfigurationManager.RefreshSection("appSettings");
            }
        }
    }
}
